package cn.itcast_05_proxy.proxyclass;

import cn.itcast_05_proxy.service.IBoss;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyBoss1 {

    @SuppressWarnings("unchecked")
    public static <T> T getProxy(final int discountCoupon,
                                 final Class<?> interfaceClass, final Class<?> implementsClass)
            throws Exception {

        return (T) Proxy.newProxyInstance(interfaceClass.getClassLoader(), new Class[]{interfaceClass}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                Integer value = (Integer) method.invoke(implementsClass.newInstance(),args);
                return value - discountCoupon;
            }
        });
    }
}
